package com.nifostasky.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nifostasky.comic.common.threadlocal.SessionLoad;
import com.nifostasky.comic.mapper.CoOtherPictureMapper;
import com.nifostasky.comic.entitys.CoOtherPicture;
import com.nifostasky.comic.service.CoOtherPictureService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (CoOtherPicture)表服务实现类
 *
 * @author 
 * @since 2022-02-15 21:14:17
 */
@Service("coOtherPictureService")
public class CoOtherPictureServiceImpl extends ServiceImpl<CoOtherPictureMapper, CoOtherPicture> implements CoOtherPictureService {

    @Override
    public List<CoOtherPicture> queryRotationPicture() {
        List<CoOtherPicture> list = this.list(new QueryWrapper<CoOtherPicture>().lambda()
                .eq(CoOtherPicture::getTypeTable,0)
                .orderByAsc(CoOtherPicture::getSort)
        );
        return list;
    }

    @Override
    public Integer addAndEditRotationPicture(CoOtherPicture pictureInfo) {
        if (Objects.isNull(pictureInfo.getId())){
            //新增
            pictureInfo.setUserId(SessionLoad.getLocalData().getUserId());
            pictureInfo.setUserName(SessionLoad.getLocalData().getUserName());
            pictureInfo.setTypeTable(0);
            this.save(pictureInfo);
        }else{
            //修改
            this.updateById(pictureInfo);
        }
        return 1;
    }

    @Override
    public Map<String, Object> queryCollectComicPage(Integer pageIndex, Integer pageSize) {
        if (Objects.isNull(pageIndex)) pageIndex = 1;
        if (Objects.isNull(pageSize)) pageSize = 10;
        Map<String,Object> map = new HashMap<>();
        Page<CoOtherPicture> page = this.page(new Page<CoOtherPicture>(pageIndex, pageSize), new QueryWrapper<CoOtherPicture>().lambda()
                .eq(CoOtherPicture::getTypeTable, 1)
                .orderByDesc(CoOtherPicture::getSort)
        );
        map.put("total",page.getTotal());
        map.put("data",page.getRecords());
        return map;
    }

    @Override
    public Integer addAndEditCollectComic(CoOtherPicture pictureInfo) {
        if (Objects.isNull(pictureInfo.getId())){
            //新增
            pictureInfo.setUserId(SessionLoad.getLocalData().getUserId());
            pictureInfo.setUserName(SessionLoad.getLocalData().getUserName());
            pictureInfo.setTypeTable(1);
            this.save(pictureInfo);
        }else{
            //修改
            this.updateById(pictureInfo);
        }
        return 1;
    }
}
